﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JU.DAL
{
    /// <summary>
    /// 登录类数据层
    /// </summary>
    public class LoginService
    {
        /// <summary>
        /// 根据登录名获得登录密码
        /// </summary>
        /// <param name="loginName">登录名</param>
        /// <returns>登录密码</returns>
        public static string GetLoginPwdByLoginName(string loginName)
        {
            SqlHelper sqlHeper = new SqlHelper();
            string sql = "SELECT LoginPwd FROM [Login] WHERE LoginName = @loginName;";
            SqlParameter par = new SqlParameter("@loginName", SqlDbType.VarChar, 50)
            {
                Value = loginName
            };
            string result = (string)sqlHeper.ExecuteScalar(sql, par);
            return result;
        }

        /// <summary>
        /// 注册用户
        /// </summary>
        /// <param name="loginName">用户名</param>
        /// <param name="loginPwd">密码</param>
        /// <returns>是否成功</returns>
        public static bool Register(JU.MODEL.Login login)
        {
            SqlHelper sqlHeper = new SqlHelper();
            string sql = "Procedure_Register";
            System.Data.SqlClient.SqlParameter[] ps =
            {
                new SqlParameter("@loginName", SqlDbType.VarChar, 50)
                {
                    Value = login.LoginName
                },
                new SqlParameter("@loginPwd", SqlDbType.VarChar, 50)
                {
                    Value = login.LoginPwd
                }
            };
            int result = 0;
            try
            {
                result = sqlHeper.ExecuteNonQuerySP(sql, ps);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result > 0;
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="login">登录信息</param>
        /// <param name="isRemeber">是否保持登录</param>
        /// <returns>是否成功</returns>
        public static bool Login(JU.MODEL.Login login, bool isRemeber)
        {
            SqlHelper sqlHeper = new SqlHelper();
            string sql = "Procedure_Login";
            System.Data.SqlClient.SqlParameter[] ps =
            {
                new SqlParameter("@loginName", SqlDbType.VarChar, 50)
                {
                    Value = login.LoginName
                },
                new SqlParameter("@loginPwd", SqlDbType.VarChar, 50)
                {
                    Value = login.LoginPwd
                },
                new SqlParameter("@isRemember", SqlDbType.Bit)
                {
                    Value = isRemeber
                },
                new SqlParameter("@isSuccess",SqlDbType.Bit),
                new SqlParameter("@loginCookie",SqlDbType.NVarChar,50),
            };
            ps[3].Direction = ParameterDirection.Output;
            ps[4].Direction = ParameterDirection.Output;
            bool result;
            try
            {
                sqlHeper.ExecuteNonQuerySP(sql, ps);
                result = (bool)ps[3].Value;
                if (result && isRemeber)
                {
                    login.LoginCookie = (string)ps[4].Value;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        /// <summary>
        /// 是否已经登录
        /// </summary>
        /// <param name="login">登录信息</param>
        /// <returns>是否已经登录</returns>
        public static bool IsLogin(JU.MODEL.Login login)
        {
            SqlHelper sqlHeper = new SqlHelper();
            string sql = "SELECT COUNT(*) FROM [Login] WHERE LoginName = @loginName AND LoginCookie=@loginCookie;";
            SqlParameter[] ps = 
            {
                new SqlParameter("@loginName", SqlDbType.VarChar, 50)
                {
                    Value = login.LoginName
                },
                new SqlParameter("@loginCookie",SqlDbType.VarChar,32)
                {
                    Value=login.LoginCookie
                }
            };
            int result = (int)sqlHeper.ExecuteScalar(sql, ps);
            return result > 0;
        }
    }
}
